﻿Partial Public Class ucAccountPolicies
    Dim dbaccountPolicy As ATSDataClassesDataContext = New ATSDataClassesDataContext


    Private Sub ucAccountPolicies_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        loadDefaults()
    End Sub
    Sub loadDefaults()
        Dim accountPolicySetting = From accountPolicy In dbaccountPolicy.admPasswordConfigs
        If accountPolicySetting.Count > 0 Then
            With accountPolicySetting.First
                textboxPasswordLength.Text = .PWLength
                If .PWExpiration > 0 Then
                    PasswordExpirationYes.IsChecked = True
                    textboxExpirationDays.Text = .PWExpiration
                Else
                    PasswordExpirationNo.IsChecked = True
                    textboxExpirationDays.IsEnabled = False
                    textboxExpirationDays.Text = 0
                End If

                If .PWLockAccount > 0 Then
                    rbLockAccountYes.IsChecked = True
                    textboxLockAccount.Text = .PWLockAccount
                Else
                    rbLockAccountNo.IsChecked = True
                End If

                cbPasswordFormatAlphaNumeric.IsChecked = .PWFormat1
                cbPasswordFormatSpecialChar.IsChecked = .PWFormat2
                cbPasswordFormatUpperCase.IsChecked = .PWFormat3
                cbPasswordFormatDifferentFromId.IsChecked = .PWDifferentFromID
            End With
        Else
            Dim accountPolicyTable As New admPasswordConfig
            With accountPolicyTable
                .PWLength = 0
                .PWExpiration = 0
                .PWFormat1 = 0
                .PWFormat2 = 0
                .PWFormat3 = 0
                .PWLockAccount = 0
                .PWDifferentFromID = 1
            End With
            dbaccountPolicy.admPasswordConfigs.InsertOnSubmit(accountPolicyTable)
            dbaccountPolicy.SubmitChanges()
        End If
        RibbonMain.isDirty = False
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub textboxPasswordLength_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxPasswordLength.TextChanged
        RibbonMain.isDirty = True
        EntryValidation.ValidateIsNumeric(textboxPasswordLength)
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Sub enablebuttons(ByVal enable As Boolean)
        buttonCancel.IsEnabled = enable
        buttonSave.IsEnabled = enable
    End Sub

    Private Sub buttonCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonCancel.Click
        loadDefaults()
    End Sub

    Private Sub buttonSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonSave.Click
        Dim accountPolicySetting = From accountPolicy In dbaccountPolicy.admPasswordConfigs
        With accountPolicySetting.First
            .PWLength = textboxPasswordLength.Text
            If PasswordExpirationNo.IsChecked Then
                .PWExpiration = 0
            Else
                .PWExpiration = textboxExpirationDays.Text
            End If
            .PWFormat1 = cbPasswordFormatAlphaNumeric.IsChecked
            .PWFormat2 = cbPasswordFormatSpecialChar.IsChecked
            .PWFormat3 = cbPasswordFormatUpperCase.IsChecked
            .PWDifferentFromID = cbPasswordFormatDifferentFromId.IsChecked
            If rbLockAccountYes.IsChecked Then
                
                .PWLockAccount = textboxLockAccount.Text
            End If

            .PWDifferentFromID = 1
        End With
        dbaccountPolicy.SubmitChanges()
        RibbonMain.isDirty = False
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub PasswordExpirationYes_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles PasswordExpirationYes.Checked
        textboxExpirationDays.IsEnabled = True
        RibbonMain.isDirty = True
        EntryValidation.ValidateIsNumeric(textboxExpirationDays)
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub textboxExpirationDays_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxExpirationDays.TextChanged
        RibbonMain.isDirty = True
        EntryValidation.ValidateIsNumeric(textboxExpirationDays)
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub PasswordExpirationNo_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles PasswordExpirationNo.Checked
        textboxExpirationDays.IsEnabled = False
        RibbonMain.isDirty = True
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub cbPasswordFormatAlphaNumeric_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbPasswordFormatAlphaNumeric.Checked, cbPasswordFormatAlphaNumeric.Unchecked
        RibbonMain.isDirty = True
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub rbLockAccountNo_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles rbLockAccountNo.Checked
        textboxLockAccount.IsEnabled = False
        RibbonMain.isDirty = True
        enablebuttons(RibbonMain.isDirty)
    End Sub

    Private Sub rbLockAccountYes_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles rbLockAccountYes.Checked
        textboxLockAccount.IsEnabled = True
        RibbonMain.isDirty = True
        EntryValidation.ValidateIsNumeric(textboxLockAccount)
        enablebuttons(RibbonMain.isDirty)

    End Sub

    Private Sub textboxLockAccount_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxLockAccount.TextChanged
        RibbonMain.isDirty = True
        EntryValidation.ValidateIsNumeric(textboxLockAccount)
        enablebuttons(RibbonMain.isDirty)
    End Sub
End Class
